package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.enums.StatusEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.Seq;
import com.ruoyi.system.domain.SysMember;
import com.ruoyi.system.domain.vo.OrderVo;
import com.ruoyi.system.mapper.SysAgentMapper;
import com.ruoyi.system.mapper.SysMemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysOrderMapper;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.service.ISysOrderService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-08
 */
@Service
public class SysOrderServiceImpl implements ISysOrderService 
{
    @Autowired
    private SysOrderMapper sysOrderMapper;

    @Autowired
    private SysAgentMapper sysAgentMapper;

    @Autowired
    private SysMemberMapper sysMemberMapper;

    /**
     * 查询订单管理
     * 
     * @param id 订单管理主键
     * @return 订单管理
     */
    @Override
    public SysOrder selectSysOrderById(Long id)
    {
        return sysOrderMapper.selectSysOrderById(id);
    }

    /**
     * 查询订单管理列表
     * 
     * @param sysOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<SysOrder> selectSysOrderList(SysOrder sysOrder)
    {
        sysOrder.setDeleteFlag(StatusEnum.NORMAL.getCode());
        return sysOrderMapper.selectSysOrderList(sysOrder);
    }

    @Override
    public List<OrderVo> selectOrderList(SysOrder sysOrder) {
        sysOrder.setDeleteFlag(StatusEnum.NORMAL.getCode());
        List<SysOrder> sysOrders = sysOrderMapper.selectSysOrderList(sysOrder);
        return sysOrders.stream().map(order ->  {
            OrderVo orderVo = new OrderVo();
            orderVo.setId(order.getId());
            orderVo.setOrderNo(order.getOrderNo());
            orderVo.setName(order.getName());
            orderVo.setMobile(order.getMobile());
            orderVo.setWechatId(order.getWechatId());
            orderVo.setNickName(order.getNickName());
            orderVo.setOpenId(order.getOpenId());
            orderVo.setCourseNo(order.getCourseNo());
            orderVo.setCourseName(order.getCourseName());
            orderVo.setDealTime(order.getDealTime());
            orderVo.setCreateTime(order.getCreateTime());
            orderVo.setLevel1Id(order.getLevel1Id());
            orderVo.setLevel2Id(order.getLevel2Id());
            orderVo.setUserStatus(order.getUserStatus());
            orderVo.setPayStatus(order.getPayStatus());
            orderVo.setPayAmount(order.getPayAmount());
            orderVo.setRefundStatus(order.getRefundStatus());
            orderVo.setOrderSource(order.getOrderSource());
            orderVo.setRefundAmount(order.getRefundAmount());
            if (Objects.nonNull(order.getLevel1Id()) && order.getLevel1Id() > 0){
                orderVo.setLevel1Name(sysAgentMapper.selectSysAgentById(order.getLevel1Id()).getName());
            }
            if (Objects.nonNull(order.getLevel2Id()) && order.getLevel2Id() > 0){
                orderVo.setLevel2Name(sysAgentMapper.selectSysAgentById(order.getLevel2Id()).getName());
            }
            return orderVo;
        }).collect(Collectors.toList());
    }

    /**
     * 新增订单管理
     * 
     * @param sysOrder 订单管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysOrder(SysOrder sysOrder)
    {
        sysOrder.setCreateTime(DateUtils.getNowDate());
        sysOrder.setOrderNo(Seq.getId());

        SysMember sysMember = new SysMember();
        sysMember.setUpdateTime(new Date());
        sysMember.setStudentFlag(1);
        LambdaQueryWrapper<SysMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMember::getMobile, sysOrder.getMobile());
        sysMemberMapper.update(sysMember,queryWrapper);
        return sysOrderMapper.insertSysOrder(sysOrder);
    }

    /**
     * 修改订单管理
     * 
     * @param sysOrder 订单管理
     * @return 结果
     */
    @Override
    public int updateSysOrder(SysOrder sysOrder)
    {
        SysOrder old = sysOrderMapper.selectSysOrderById(sysOrder.getId());
        sysOrder.setUpdateTime(DateUtils.getNowDate());
        return sysOrderMapper.updateSysOrder(sysOrder);
    }

    /**
     * 批量删除订单管理
     * 
     * @param ids 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteSysOrderByIds(String ids)
    {
        return sysOrderMapper.deleteSysOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除订单管理信息
     * 
     * @param id 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteSysOrderById(Long id)
    {
        return sysOrderMapper.deleteSysOrderById(id);
    }
}
